package com.yanqu.road.dao.impl.activity.goddess;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.goddess.config.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class GoddessConfigDaoImpl extends TempDao {

    private static String getGoddessEraInfoSql = "select * from `t_s_activity_fixsky_era` where `activityId`=? ORDER BY `eraId` ASC";
    public List<GoddessEraInfo> getGoddessEraInfoList(int activityId) {
        List<GoddessEraInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGoddessEraInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoddessEraInfo data = new GoddessEraInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setEraId(rs.getInt("eraId"));
                data.setUpNeed(rs.getString("upNeed"));
                data.setEnergyMax(rs.getInt("energyMax"));
                data.setEnergyResume(rs.getInt("energyResume"));
                data.setEraReward(rs.getString("eraReward"));
                data.setPeopleUpParam(rs.getInt("peopleUpParam"));
                data.setPointParam(rs.getString("pointParam"));
                data.setMakeNum(rs.getString("makeNum"));
                data.setPeopleNum(rs.getInt("peopleNum"));
                data.setEnergyCost(rs.getInt("energyCost"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGoddessLiangYiStoneInfoSql = "select * from `t_s_activity_fixsky_liangyi` where `activityId`=? ORDER BY `liangYiLv` ASC";
    public List<GoddessLiangYiStoneInfo> getGoddessLiangYiStoneInfoList(int activityId) {
        List<GoddessLiangYiStoneInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGoddessLiangYiStoneInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoddessLiangYiStoneInfo data = new GoddessLiangYiStoneInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setLiangYiLv(rs.getInt("liangYiLv"));
                data.setUpCost(rs.getInt("upCost"));
                data.setAddEffect(rs.getInt("addEffect"));
                data.setTurnNum(rs.getInt("turnNum"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGoddessRandomStoneInfoSql = "select * from `t_s_activity_fixsky_randomstone` where `activityId`=? ORDER BY `shapeId` ASC";
    public List<GoddessRandomStoneInfo> getGoddessRandomStoneInfoList(int activityId) {
        List<GoddessRandomStoneInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGoddessRandomStoneInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoddessRandomStoneInfo data = new GoddessRandomStoneInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setShapeId(rs.getInt("shapeId"));
                data.setShape(rs.getString("shape"));
                data.setShapeType(rs.getInt("shapeType"));
                data.setShapePoint(rs.getInt("shapePoint"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGoddessMissionInfoSql = "select * from t_s_activity_fixsky_mission  where `activityId`=? ORDER BY `missionId` ASC";
    public List<GoddessMissionInfo> getGoddessMissionInfoList(int activityId) {
        List<GoddessMissionInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGoddessMissionInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoddessMissionInfo data = new GoddessMissionInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setMissionId(rs.getInt("missionId"));
//                data.setEnergyCost(rs.getInt("energyCost"));
                data.setLandInfo(rs.getString("landInfo"));
                data.setTargetPoint(rs.getString("targetPoint"));
                data.setShapeWeight(rs.getString("shapeWeight"));
                data.setPeopleUpParam(rs.getInt("peopleUpParam"));
                data.setReward(rs.getString("reward"));
                data.setBackId(rs.getInt("backId"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGoddessPeoBookInfoSql = "select * from `t_s_activity_fixsky_peobook` where `activityId`=? ORDER BY `bookId` ASC";
    public List<GoddessPeoBookInfo> getGoddessPeoBookInfoList(int activityId) {
        List<GoddessPeoBookInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGoddessPeoBookInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoddessPeoBookInfo data = new GoddessPeoBookInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setBookId(rs.getInt("bookId"));
                data.setComb(rs.getString("comb"));
                String val = rs.getString("combName");
                if(!StringUtils.isNullOrEmpty(val)){
                    data.setCombName(val);
                }
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGoddessPeopleInfoSql = "select * from `t_s_activity_fixsky_people` where `activityId`=? ORDER BY `peopleId` ASC";
    public List<GoddessPeopleInfo> getGoddessPeopleInfoList(int activityId) {
        List<GoddessPeopleInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGoddessPeopleInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoddessPeopleInfo data = new GoddessPeopleInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setPeopleId(rs.getInt("peopleId"));
                data.setName(rs.getString("name"));
                data.setDesc(rs.getString("desc"));
                data.setWeight(rs.getString("weight"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
